import store from "@/store/index.js"
const user = {
	state: {
		//登录状态
		loginStatus:false,
		//token
		token:undefined,
		//用户的信息(昵称/头像)
		userInfo:{}
	},
	mutations: {
		//进入app就执行这个方法,读取用户信息
		initUser(state){
			let userInfo = uni.getStorageSync('userInfo')
			if(userInfo){
				userInfo = JSON.parse(userInfo)
				state.userInfo = userInfo
				state.loginStatus = true
				state.token = userInfo.token
			}
		},
		//登录后保存用户信息
		login(state,userInfo){
			state.loginStatus = true
			state.userInfo = userInfo
			state.token = userInfo.token
			//持久化储存
			uni.setStorageSync('userInfo',JSON.stringify(userInfo))
			store.commit('initPath')
			store.commit('initCart')
		},
		//退出登录
		loginOut(state){
			state.loginStatus = false
			state.token = undefined
			state.userInfo = {}
			uni.removeStorageSync('userInfo')
		}
	},
	actions: {
	
	},
	getters: {

	}
}
export default user
